﻿/*	
	stencil - action script 3 isometric game engine.

	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.ui 
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import stencil.core.ui.KeyboardKeys;
	
	
	[Event(name="keyUp",type="flash.events.KeyboardEvent")]
	
	[Event(name="keyDown",type="flash.events.KeyboardEvent")]
	
	/**
	 * Provides means for handling keyboard input and for indicating it current state.
	 * 
	 * @author
	 */
	public class KeyboardController extends EventDispatcher implements IKeyboard {
		
		private var _keyboardEventSource:IEventDispatcher
		
		private var _keyCodes:Array = new Array();
		
		private var _releasedKeyCodesTemp:Array = new Array();
		
		private var _releasedKeyCodes:Array = new Array();
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		/**
		 * Creates new instance of KeyboardContoller.
		 * @param	keyboardEventSource	the keyboard event dispatcher to listen.
		 */
		public function KeyboardController(keyboardEventSource:IEventDispatcher) {
			this._keyboardEventSource = keyboardEventSource;
			this._keyboardEventSource.addEventListener(KeyboardEvent.KEY_UP, this.keyUpHandler);
			this._keyboardEventSource.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler);
			this._keyboardEventSource.addEventListener(Event.DEACTIVATE, this.deactivateHandler);
		}
		
		//------------------------------------------------------------------------------------------------
		// Public properties
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function get anyKeyDown():Boolean {
			return this._keyCodes.length > 0;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get pressedKeyCodes():Array {
			return this._keyCodes;
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function get capsLock():Boolean {
			return Keyboard.capsLock;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get numLock():Boolean {
			return Keyboard.numLock;
		}
		
		/**
		 * @inheritDoc
		 */
		public function isKeyDown(keyCode:uint):Boolean {
			return this._keyCodes.indexOf(keyCode) >= 0;
		}
		
		/**
		 * @inheritDoc
		 */
		public function areKeysDown(keyCodes:Array):Boolean {
			for each(var keyCode:uint in keyCodes) {
				if (!this.isKeyDown(keyCode)) {
					return false;
				}
			}
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function isKeyUp(keyCode:int):Boolean {
			return !this.isKeyDown(keyCode);
		}
		
		/**
		 * @inheritDoc
		 */
		public function isKeyReleased(keyCode:int):Boolean {
			return this._releasedKeyCodes.indexOf(keyCode) >= 0;
		}
		
		/**
		 * @inheritDoc
		 */
		public function update():void {
			
			if (this._releasedKeyCodes.length > 0) {
				while (this._releasedKeyCodes.length) {
					this._releasedKeyCodes.shift();
				}
			}
			
			if (this._releasedKeyCodesTemp.length > 0) {
				while (this._releasedKeyCodesTemp.length) {
					this._releasedKeyCodes.push(this._releasedKeyCodesTemp.shift());
				}
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function unsubscribe():void {
			this._keyCodes = [];
			this._keyboardEventSource.removeEventListener(KeyboardEvent.KEY_UP, this.keyUpHandler);
			this._keyboardEventSource.removeEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler);
			this._keyboardEventSource.removeEventListener(Event.DEACTIVATE, this.deactivateHandler);
		}
		
		
		//------------------------------------------------------------------------------------------------
		// Private methods
		//------------------------------------------------------------------------------------------------
		
		private function keyUpHandler(event:KeyboardEvent):void {
			var keyCode:uint = event.keyCode;
			var index:int = this._keyCodes.indexOf(keyCode);
			
			if (index >= 0) {
				this._keyCodes.splice(index, 1);
				this._releasedKeyCodesTemp.push(keyCode);
			}
			
			this.dispatchEvent(event);
		}
		
		private function keyDownHandler(event:KeyboardEvent):void {
			var keyCode:uint = event.keyCode;
			
			if (this._keyCodes.indexOf(keyCode) == -1) {
				this._keyCodes.push(keyCode);
			}
			this.dispatchEvent(event);
		}
		
		private function deactivateHandler(event:Event):void {
			if (this._keyCodes.length > 0) {
				this._releasedKeyCodesTemp = this._keyCodes;
				this._keyCodes = [];
			}
			this.dispatchEvent(event);
		}
		
		
	}

}